/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package br.liveo.ndrawer.slice;

import br.liveo.ndrawer.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;

import ohos.app.Context;

import java.util.Optional;

/**
 * SettingAbilitySlice
 */
public class SettingAbilitySlice extends AbilitySlice {
    private Image back;
    private Integer statusBarHeight;
    private Component statusBar;
    private int windowWith;
    private int windowHeight;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_setting);
        if (getHeight(this) > 129) {
            statusBarHeight = getHeight(this) - 126;
        } else {
            statusBarHeight = getHeight(this);
        }
        Optional<Display>
                display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
        Point pt = new Point();
        display.get().getSize(pt);
        windowWith = display.get().getAttributes().width;
        windowHeight = display.get().getAttributes().height;
        statusBar = findComponentById(ResourceTable.Id_statusBar);
        initStatusBar();
        back = (Image) findComponentById(ResourceTable.Id_leftImage_one);
        back.setClickedListener(component -> terminate());
    }

    private Integer getHeight(Context context) {
        Point point = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getSize(point);
        Point point1 = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getRealSize(point1);
        return point1.getPointYToInt() - point.getPointYToInt();
    }

    private void initStatusBar() {
        if (windowWith > windowHeight) {
            if (statusBarHeight == 0) {
                statusBarHeight = 100;
            }
        }
        statusBar.setHeight(statusBarHeight);
    }
}
